package com.ym.jiujiangeyou.fragment;

import java.net.ContentHandler;

import com.ym.jiujiangeyou.R;
import com.ym.jiujiangeyou.ui.ActionItem;
import com.ym.jiujiangeyou.ui.TitlePopup;
import com.ym.jiujiangeyou.ui.TitlePopup.OnItemOnClickListener;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
public class MineFragment extends Fragment {
	
		
		//定义标题栏弹窗按钮
    private TitlePopup titlePopup;
	private ListView lvContent;// PopupWindow显示的View中的ListView
	private ImageView collect_del_img ;
	private Fragment content ;
	
	
	public MineFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.mine, null, false);
		init(view) ;
		initData();
		/*if (savedInstanceState != null) {
			content = getActivity().getSupportFragmentManager().getFragment(savedInstanceState, "content") ;
		}*/
			content = new MineHistoryFragment() ;
		android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager() ;
		fm.beginTransaction().replace(R.id.content_frame, content).commit() ;
		return view;

	}
	
	@SuppressWarnings("rawtypes")
	public void init(View view){
		TextView title_text = (TextView) view.findViewById(R.id.title_text);
		title_text.setText(R.string.title_text_mine);
		collect_del_img = (ImageView) view.findViewById(R.id.collect_del_img) ;
		
		collect_del_img.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				titlePopup.show(v);
			}
		}) ;
		//实例化标题栏弹窗
				titlePopup = new TitlePopup(getActivity(), LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	
				titlePopup.setItemOnClickListener(new OnItemOnClickListener() {
					
					@Override
					public void onItemClick(ActionItem item, int position) {
						// TODO Auto-generated method stub
					//	Toast.makeText(getActivity(), item.titleId, Toast.LENGTH_SHORT).show() ;
						Fragment fragment = null ;
						switch (item.titleId) {
						case  R.string.mine_pop_title1:
							fragment = new MineHistoryFragment()  ;
							break;
						case  R.string.mine_pop_title2:
							fragment = new MinePupTwoFragment() ;
							break;
						case  R.string.mine_pop_title3:
							fragment = new MinePupThreeFragment() ;
							break;
						case  R.string.mine_pop_title4:
							fragment = new MinePupFour() ;
							break;
						case  R.string.mine_pop_title5:
							fragment = new MinePupFiveFragment();
							break;
						case  R.string.mine_pop_title6:
							fragment = new MinePupSixFragment();
							break;
						case  R.string.mine_pop_title7:
							Toast.makeText(getActivity(), "7", Toast.LENGTH_SHORT).show() ;
							break;
						default:
							break;
						}
						if(fragment != null){
							switchContent(fragment) ;
						}
						
					}
				}) ;
				
		
				
	}
	
	/** 
	* @param fragment  
	* @retur  void 
	* @Description:切换视图
	*/
	public void switchContent(Fragment fragment){
		content = fragment ;
		getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit() ;
	}
	
	/**
	 * 初始化数据
	 */
	private void initData(){
		//给标题栏弹窗添加子类
		//titlePopup.addAction(new ActionItem(getActivity(), "发起聊天", R.drawable.mm_title_btn_compose_normal));
	//	titlePopup.addAction(new ActionItem(getActivity(), "听筒模式", R.drawable.mm_title_btn_receiver_normal));
	//	titlePopup.addAction(new ActionItem(getActivity(), "登录网页", R.drawable.mm_title_btn_keyboard_normal));
	//	titlePopup.addAction(new ActionItem(getActivity(), "扫一扫",  R.drawable.mm_title_btn_qrcode_normal));
		
		titlePopup.addAction(new ActionItem(getActivity(), R.string.mine_pop_title1));
		titlePopup.addAction(new ActionItem(getActivity(), R.string.mine_pop_title2));
		titlePopup.addAction(new ActionItem(getActivity(), R.string.mine_pop_title3));
		titlePopup.addAction(new ActionItem(getActivity(), R.string.mine_pop_title4));
		titlePopup.addAction(new ActionItem(getActivity(), R.string.mine_pop_title5));
		titlePopup.addAction(new ActionItem(getActivity(), R.string.mine_pop_title6));
		titlePopup.addAction(new ActionItem(getActivity(), R.string.mine_pop_title7));
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
	// TODO Auto-generated method stub
	super.onSaveInstanceState(outState);
	//getActivity().getSupportFragmentManager().putFragment(outState, "content", content) ;
	}
	
	
}
